<?php
/**
 * Only for testing purpose
 **/
// {{{ test

    public function test_index() {
      $opt = array(
        'page'  => '2',
        'count' => '12',
        'order' => 'releasedate desc,rating desc',
      );
      $field = ['id','title','releasedate','rating','ratingnum','mpaa','genre'];
      $data = A('Common/Movie')->getTopNewMovies($opt, $field);
      dump($data);
    }


    public function test_genre_index() {
      $opt = array(
        'genre_field'   => 'id,name,description',
        'genre_cond'    => array(
          'name'  => 'adventure', // or 'name' => array('eq', 'adventure')
        ),
        'movie_field'   => 'id,title,releasedate,rating,poster',
        'movie_order'   => 'rating desc,releasedate desc',
        'page'          => '2',
        'count'         => '12',
      );
      $data = A('Common/Genre')->getTopNewMovies($opt);
      dump($data);
    }


    public function test_genre_get() {
      //$field = ['id', 'name']; // ok
      $field = 'id,name'; // ok
      //$name = 'adventure'; // ok
      //$name = 'adventure,drama'; // ok for only ',' split WITHOUT whitespace!
      $name = ['adventure', 'drama'];
      $data = A('Common/Genre')->getGenreByName($name, $field);
      dump($data);
    }


    public function test_person_rel_index() {
      $opt = array(
        //'rel'   => 'movies',
        //'rel'   => 'castmovies',
        'rel'   => 'crewmovies',
      );
      //$data = A('Common/Person')->getPersonRelById('p0000040', $opt);
      //$data = A('Common/Person')->getPersonRelById('p0000245', $opt); // this person has actor & director in different movies
      $data = A('Common/Person')->getPersonRelById('p0000017', $opt); // this person has actor & director in different movies
      dump($data);
    }


// =======================================

    public function test_render() {
      //dump(T('Common/default'));
      $this->display('Common/default');
    }

    public function test_render_index() {
      $opt = array(
        'page'  => '1',
        'count' => '12',
        'order' => 'rating desc',
      );
      $field = ['id','title','releasedate','rating','ratingnum','mpaa','genre', 'poster'];
      $data = A('Common/Movie')->getTopNewMovies($opt, $field);
      //trace($data, '[movies]', 'debug');

      $genre = A('Common/Genre')->getGenres(array(
      'field' => 'id,name', 'limit' => '6'));

      $this->assign('data', $data);
      $this->assign('genre', $genre);
      $this->assign('spec_content', 'Movie/movie-slides');
      $this->display('Movie/index-base');
    }

    public function test_render_index_page() {
      $opt = array(
        'page'  => '2',
        'count' => '12',
        'order' => 'releasedate desc,rating desc',
      );
      $field = ['id','title','releasedate','rating','ratingnum','mpaa','genre', 'poster'];
      $data = A('Common/Movie')->getTopNewMovies($opt, $field);

      $genre = A('Common/Genre')->getGenres(array(
      'field' => 'id,name', 'limit' => '6'));

      $count = M('Movie')->count();
      $page = new \Org\Util\BootStrapPage($count, 12);
      //$page = new \Think\Page($count, 12);

      $this->assign('data', $data);
      $this->assign('genre', $genre);
      $this->assign('spec_content', 'Movie/movie-slides');
      $this->assign('page', $page->show());

      $this->display('Movie/index-base');
    }

    public function test_render_genre_index() {
      $opt = array(
        'genre_field'   => 'id,name,description',
        'genre_cond'    => array(
          'name'  => 'adventure', // or 'name' => array('eq', 'adventure')
        ),
        'movie_field'   => 'id,title,releasedate,rating,poster',
        'movie_order'   => 'rating desc,releasedate desc',
        'page'          => '2',
        'count'         => '12',
      );
      $data = A('Common/Genre')->getTopNewMovies($opt);

      $genre = A('Common/Genre')->getGenres(array(
      'field' => 'id,name', 'limit' => '6'));

      $this->assign('data', $data['movies']);
      $this->assign('genre', $genre);
      $this->assign('current_genre', array(
        'name' => $data['name'],
        'description' => $data['description'],
      ));
      $this->assign('spec_content', 'Movie/movie-genre');
      $this->display('Movie/index-base');
    }

    public function test_render_single_movie() {
      $opt = array(
        'rel' => ['genres', 'director', 'persons'],
      );
      //$field = 'id,title,rating,producedby,poster';
      $field = '';
      $data = A('Common/Movie')->getMovieRelById('v0000001', $opt, $field);

      //$data['producedby'] = explode(',', $data['producedby']);

      $this->assign('data', $data);
      $this->display('Movie/single-movie');
    }

    public function test_render_single_person() {
      $opt = array(
        'rel' => ['genres', 'movies'],
      );
      $field = '';
      $data = A('Common/Person')->getPersonRelById('p0000017', $opt, $field);

      $this->assign('data', $data);
      $this->display('Person/single-person');
    }

    public function test_render_genre_list() {
      $opt = array(
        'field' => ['id', 'name'],
      );
      $data = A('Common/Genre')->getGenres($opt);
      $this->assign('data', $data);
      $this->display('Movie/genre-list');
    }

    public function test_render_search($name/*$title*/) {
      //$title = trim($title);
      //$_cond['title,aka'] = $title;
      $_cond['name,aka'] = $name;
      $_opt = array(
        'offset'  => 0,
        'size'    => 4,
        //'rel'     => 'director',
      );
      //$_field = 'id,title,aka,poster,genre,releasedate';
      $_field = 'id,name,aka,poster,occupation,birthdate,deathdate';
      //$Movie = A('Common/Movie');
      $Person = A('Common/Person');

      //list($count, $data) = $Movie->getMoviesByCond($_cond, $_opt, $_field, true);
      list($count, $data) = $Person->getPersonsByCond($_cond, $_opt, $_field, true);

      //$this->assign('search_movie', true);
      $this->assign('search_person', true);
      $this->assign('data', $data);
      $this->assign('count', $count);
      //$this->assign('query', $title);
      $this->assign('query', $name);
      $page = new \Org\Util\BootStrapPage($count, 4);
      $this->assign('page', $page->show());
      //$this->assign('result', 'Search/result-movie');
      $this->assign('result', 'Search/result-person');

      $this->display('Search/result-base');
    }


    public function test_render_findr() {
      $this->display('Search/advance-movie');
    }

    public function test_render_findr_result() {
      /*$title = trim($title);
      $_cond['title,aka'] = $title;
      $_opt = array(
        'offset'  => 0,
        'size'    => 20,
        //'rel'     => 'director',
      );
      $_field = 'id,title,aka,poster,genre,releasedate';
      $Movie = A('Common/Movie');

      $data = $Movie->getMoviesByCond($_cond, $_opt, $_field, false);

      $this->assign('data', $data);
      $this->display('Search/result-advance');*/
      $this->assign('js', 'masonry');
      $this->display('Search/result-advance');
    }


    public function test_url_para() {
      dump($_GET);
    }

    public function test_complex_query($title, $filter, $include='', $offset=0, $size=5/*20*/) {
      $title = trim($title);
      if(empty($title)) return;
      $_cond = array();

      // handle title
      $_cond['title,aka'] = $title;
      // handle filter
      $_marker_list = array(
        '<:'  => 'elt', 
        '>:'  => 'egt', 
        '>'   => 'gt', 
        '<'   => 'lt', 
        ':'   => '',
      );
      foreach($filter as $_it) {
        foreach($_marker_list as $_marker => $_op) {
          if(strpos($_it, $_marker)) {
            list($_field, $_val) = explode($_marker, $_it, 2);
            $_op ? ($_cond[$_field][] = array($_op, $_val)) : 
                   ($_cond[$_field][] = $_val);
            break;
          }
        }
      }
      dump($_cond);
      $_cond['mpaa'] = array('IN', implode(',',$_cond['mpaa']));

      $_opt = array(
        'offset'  => $offset,
        'size'    => $size,
        'rel'     => 'director',
      );

      $_field = $include;

      dump($_cond);
      $data = A('Common/Movie')->getMoviesByCond($_cond, $_opt, $_field);
      dump($data);
    }
// }}}
